package com.ruibang.glass.equipment.service.impl.device;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mysql.jdbc.StringUtils;
import com.ruibang.glass.equipment.constant.Constants;
import com.ruibang.glass.equipment.constant.EquipmentErrorCode;
import com.ruibang.glass.equipment.domain.req.device.DeviceAllPageReq;
import com.ruibang.glass.equipment.dto.SelectDTO;
import com.ruibang.glass.equipment.dto.SelectNewDTO;
import com.ruibang.glass.equipment.entity.device.InventoryManagement;
import com.ruibang.glass.equipment.entity.device.SparePartsInventory;
import com.ruibang.glass.equipment.mapper.device.InventoryManagementMapper;
import com.ruibang.glass.equipment.mapper.device.SparePartsInventoryMapper;
import com.ruibang.glass.equipment.service.InventoryManagementService;
import com.teaming.cloud.framework2.common.exception.ServiceException;
import com.teaming.cloud.framework2.common.model.ResultBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 检修工作单服务类
 * </p>
 *
 * @author mameng
 * @since 2024-03-20
 */
@Slf4j
@Service
public class InventoryManagementServiceImpl extends ServiceImpl<InventoryManagementMapper, InventoryManagement> implements InventoryManagementService {

    @Autowired
    private SparePartsInventoryMapper sparePartsInventoryMapper;

    /**
     * 检修工作单列表
     * @param deviceAllPageReq
     * @return
     */
    @Override
    public IPage<InventoryManagement> getPointRecordList(DeviceAllPageReq deviceAllPageReq) {
        IPage page = baseMapper.getCheckList(new Page<>(deviceAllPageReq.getPageNo(), deviceAllPageReq.getPageSize()), deviceAllPageReq);
        return page;
    }

    /**
     * 新增跟新
     * @param repairPlan
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultBody addOrUpdateCheck(InventoryManagement repairPlan) {
        SparePartsInventory sparePartsInventory = sparePartsInventoryMapper.selectOne(Wrappers.<SparePartsInventory>lambdaQuery()
                .eq(SparePartsInventory::getIsDelete, Constants.ZERO_STR).eq(SparePartsInventory::getSparePartsName, repairPlan.getSparePartsName()).orderByDesc(SparePartsInventory::getCreateTime).last("limit 1"));
        if(sparePartsInventory==null){
            throw new ServiceException(EquipmentErrorCode.SPARE_PARTS_INFO_NAME_IS_EXIST.getCode(),EquipmentErrorCode.SPARE_PARTS_INFO_NAME_IS_EXIST.getMsg());
        }

        if (sparePartsInventory.getStockQuantity()==null||sparePartsInventory.getStockQuantity()==0) {
            sparePartsInventory.setStockQuantity(repairPlan.getInboundQuantity());
            sparePartsInventoryMapper.updateById(sparePartsInventory);
        } else {
            Integer count = sparePartsInventory.getStockQuantity() + repairPlan.getInboundQuantity();
            sparePartsInventory.setStockQuantity(count);
            sparePartsInventoryMapper.updateById(sparePartsInventory);
        }
        if (StringUtils.isNullOrEmpty(repairPlan.getId())) {
            baseMapper.insert(repairPlan);
            return ResultBody.success();
        }
        baseMapper.updateById(repairPlan);
        return ResultBody.success();
    }

    /**
     * 检修工作单 下拉框
     * @return
     */
    @Override
    public ResultBody getNameAndId() {
        List<SelectDTO> selectDTOList = new ArrayList<>();
        List<InventoryManagement> inventoryManagements = baseMapper.selectList();
        for (InventoryManagement inventoryManagement : inventoryManagements) {
            SelectDTO selectDTO = new SelectDTO();
            selectDTO.setLabel(inventoryManagement.getSparePartsName());
            selectDTO.setValue(inventoryManagement.getId());
            selectDTOList.add(selectDTO);
        }
        return ResultBody.success(selectDTOList);
    }

    @Override
    public ResultBody getNumberAndId() {
        List<SelectDTO> selectDTOList = new ArrayList<>();
        List<InventoryManagement> inventoryManagements = baseMapper.selectList();
        for (InventoryManagement inventoryManagement : inventoryManagements) {
            SelectDTO selectDTO = new SelectDTO();
            selectDTO.setLabel(String.valueOf(inventoryManagement.getInboundQuantity()));
            selectDTO.setValue(inventoryManagement.getId());
            selectDTOList.add(selectDTO);
        }
        return ResultBody.success(selectDTOList);
    }

    @Override
    public ResultBody getNumberAndName() {
        List<SelectNewDTO> selectDTOList = new ArrayList<>();
        List<InventoryManagement> inventoryManagements = baseMapper.selectList();
        for (InventoryManagement inventoryManagement : inventoryManagements) {
            SelectNewDTO selectDTO = new SelectNewDTO();
            selectDTO.setLabel(inventoryManagement.getSparePartsName());
            selectDTO.setValue(inventoryManagement.getId());
            selectDTO.setNumber(String.valueOf(inventoryManagement.getInboundQuantity()));
            selectDTOList.add(selectDTO);
        }
        return ResultBody.success(selectDTOList);
    }


}
